#include "stdafx.h"
#include "ToolbarWindow.h"
#include "resource.h"

CToolbarWindow::CToolbarWindow()
    : m_hImageList(NULL)
    , m_pIWebBrowser2(NULL)
{

}

CToolbarWindow::~CToolbarWindow()
{
    ReleaseResources();
}

LRESULT CToolbarWindow::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
    BOOL success = FALSE;

    if (lParam)
    {
        LPCREATESTRUCT lpCreateStruct = LPCREATESTRUCT(lParam);
        m_pIWebBrowser2 = static_cast<IWebBrowser2*>(lpCreateStruct->lpCreateParams);
        if (m_pIWebBrowser2)
        {
            m_pIWebBrowser2->AddRef();

            // Set size of TBBUTTON structure.
            SendMessage(m_hWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);

            // Set maximum number of text rows.
            SendMessage(m_hWnd, TB_SETMAXTEXTROWS, 1, 0L);

            // TODO: get title(s), icon(s) and control id(s)from configuration file, 
            // from an entry fitting the and target url in the command handler.

            WCHAR* pButtonTitle = _T("Home page");
            int firstStringIndex = ::SendMessage(m_hWnd, TB_ADDSTRING, 0,(LPARAM)pButtonTitle);
            if (firstStringIndex != -1)
            {
                HICON hAppIcon = LoadIcon(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDI_APP_ICON));
                if (hAppIcon)
                {
                    m_hImageList = ImageList_Create(16,16, ILC_COLOR16, 1, 0);
                    if (m_hImageList)
                    {
                        int newImageIndex = ImageList_AddIcon(m_hImageList, hAppIcon);
                        if (newImageIndex != -1)
                        {
                            ::DestroyIcon(hAppIcon);
                            ::SendMessage(m_hWnd, TB_SETIMAGELIST, 0, (LPARAM)m_hImageList);

                            TBBUTTON Button		= {0};
                            Button.idCommand	= IDC_GOTO_WEBSITE;
                            Button.fsState		= TBSTATE_ENABLED;
                            Button.dwData		= 0;
                            Button.iString		= newImageIndex;
                            Button.iBitmap		= 0;
                            success				= ::SendMessage(m_hWnd, TB_INSERTBUTTON, 0, (LPARAM)&Button);
                        }
                    }
                }
            }
        }
    }
    
    ATLASSERT(success);
    return success ? 0 : -1;
}

LRESULT CToolbarWindow::OnGotoWebsite(WORD wNotifyCode, WORD wID, HWND hwndCtl, BOOL& bHandled)
{
    ATLASSERT(m_pIWebBrowser2);

    if (m_pIWebBrowser2)
    {
        // TODO: get the target url from a configuration file.
        CComBSTR targetUrl(L"http://gamersweb2.appspot.com");
        NavigateBrowser(targetUrl);
    }
    
    return 0;
}

VOID CToolbarWindow::ReleaseResources()
{
    if (IsWindow())
    {
        DestroyWindow();
    }

    if (m_hImageList)
    {
        ::ImageList_Destroy(m_hImageList);
        m_hImageList = NULL;
    }

    if (m_pIWebBrowser2)
    {
        m_pIWebBrowser2->Release();
        m_pIWebBrowser2 = NULL;
    }
}

HRESULT CToolbarWindow::NavigateBrowser(BSTR targetUrl)
{
    HRESULT hr = E_FAIL;

    if (m_pIWebBrowser2)
    {
        m_pIWebBrowser2->Stop();

        VARIANT vEmpty;
        VariantInit(&vEmpty);
        hr = m_pIWebBrowser2->Navigate(targetUrl, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
    }

    return hr;
}